package org.agile2soft.nexoma.core.tapestry.encoder;

import org.agile2soft.nexoma.core.controller.InternalUserController;
import org.agile2soft.nexoma.core.entity.InternalUser;
import org.apache.tapestry5.EventContext;
import org.apache.tapestry5.ValueEncoder;
import org.hibernate.SessionFactory;

import br.com.arsmachina.tapestrycrud.encoder.Encoder;
import br.com.arsmachina.tapestrycrud.hibernate.encoder.HibernatePrimaryKeyEncoder;

public class InternalUserEncoder extends HibernatePrimaryKeyEncoder<InternalUser,String>
implements Encoder<InternalUser,String>{

    public InternalUserEncoder(InternalUserController controller,
            SessionFactory sessionFactory) {
        super(controller, sessionFactory);
    }

    public String toLabel(InternalUser user) {
        return user.getLogin();
    }

    public Class<String> getKeyType() {
        return null;
    }

    public String toClient(InternalUser user) {
        return (user.getDocumentNumber()==null)?"":user.getDocumentNumber();
    }

    public ValueEncoder<InternalUser> create(Class<InternalUser> type) {
        return this;
    }

    public Object toActivationContext(InternalUser object) {
        return toClient(object);
    }

    public InternalUser toObject(EventContext eventContext) {
        InternalUser user = null;
        if ( eventContext.getCount()> 0 ) {
            user = getController().findById(eventContext.get(String.class, 0));
        }
        return user;
    }


}
